package kasgroup2;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * A simple (incomplete) Table Manager for the Java Swing Table Component (JTable)
 * 
 * An article on how to use Java Tables:
 * http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
 * 
 * Example Usage:
 * 
 * TableManager tm = new TableManager(jTable1)
 * 
 * 
 * 
 * 
 * @author Benjamin Hammer
 * @version 2012-05-09
 */
public class TableManager {
    
    private JTable table = null;
    private DefaultTableModel model = null;
    
    public TableManager(JTable p_Table) {
        this.table = p_Table;
        this.model = (DefaultTableModel)p_Table.getModel();
        table.setModel(model);
    }
    
    public int getRowCount() {
        return table.getRowCount();
    }
    
    public void setCellValue(Object value, int row, int column) {
        table.setValueAt(value, row, column);
    }
    
    public Object getCellValue(int row, int column) {
        return table.getValueAt(row, column);
    }
    
    public void addRow(Object[] rowData) {
        model.insertRow(table.getRowCount(), rowData);
    }
    
    public void removeRow(int rowIndex) {
        model.removeRow(rowIndex);
    }
    
    public int getSelectedRowIndex() {
        return table.getSelectedRow();
    }
    
    public void removeSelectedRow() {
        int selRow = table.getSelectedRow();
        if(selRow >= 0) {
            removeRow(selRow);
        }
    }
    
    public Object getCellValueForSelectedRow(int column) {
        Object cellValue = "";
        int selRow = table.getSelectedRow();
        //System.out.println("Selected row: " + selRow + " - name: " + getCellValue(selRow, column));
        if(selRow >= 0) {
            cellValue = getCellValue(selRow, column);
        }
        return cellValue;
    }
    
    /**
     * Reset the table data
     * http://stackoverflow.com/questions/3879610/clear-contents-of-a-jtable
     */
    public void clear() {
        model.setRowCount(0);
    }

    public int[] getSelectedRows() {
        return table.getSelectedRows();
    }
}
